<template>
  <a-modal
    title="修改型号"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="modelEditVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="终端型号代码"
                       v-bind="formItemLayout">
            <a-input v-decorator="['modelCode',{rules: [{ required: true, message: '终端品牌代码不能为空'}]}]" :disabled="true"/>
          </a-form-item>
          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label='终端型号名称'
                       v-bind="formItemLayout">
            <a-input v-decorator="['modelName',{rules: [{ required: true, message: '终端品牌名称不能为空'}]}]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="终端型号描述"
                       v-bind="formItemLayout">
            <a-input v-decorator="['modelDesc']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label='终端品牌代码'
                       v-bind="formItemLayout">
            <a-input :disabled="true" v-decorator="['brandCode',{rules: [{ required: true, message: '联系人姓名不能为空'}]}]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label='联系人姓名'
                       v-bind="formItemLayout">
            <a-input v-decorator="['contactName',{rules: [{ required: true, message: '联系人姓名不能为空'}]}]"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="联系电话"
                       v-bind="formItemLayout">
            <a-input v-decorator="['contactPhone', {rules: [{required: true, message: '请输入联系电话'},
            { pattern: '^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$', message: '请输入正确的联系电话'}
          ]}]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label='联系人电子邮箱'
                       v-bind="formItemLayout">
            <a-input v-decorator="['contactEmail',{rules: [
            { type: 'email', message: '请输入正确的邮箱' },
            { max: 50, message: '长度不能超过50个字符'}
          ]}]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <br>
    <div class="drawer-bootom-button">
      <a-button @click="onClose" style="margin-right: .8rem">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 6},
    wrapperCol: {span: 18}
  }
  export default {
    name: 'ModelEdit',
    props: {
      modelEditVisiable: {
        default: false
      },
      modelInfoData: {
        require: true
      },
      modelInstId: '',

    },
    data() {
      return {
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        model: {
          menuId: ''
        },
        validateStatus: '',
        checkStrictly: false
      }
    },
    computed: {
      show: {
        get: function () {
          return this.modelEditVisiable
        },
        set: function () {
        }
      }
    },
    methods: {
      reset() {
        this.menuTreeKey = +new Date()
        this.expandedKeys = []
        this.checkedKeys = []
        this.defaultCheckedKeys = []
        this.menuSelectStatus = this.menuSelectHelp = ''
        this.loading = false
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      setModelFormValues({...model}) {
        let fields = ['id', 'modelCode', 'modelName', 'modelDesc', 'brandCode', 'contactName', 'contactPhone', 'contactEmail']
        Object.keys(model).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = model[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.loading = true
            let model = this.form.getFieldsValue()
            model.instId = this.modelInstId
            this.model.flag = 'upd'
            // console.log(model)
            this.$put('modelAdmin', {
              ...model
            }).then((r) => {
              this.reset()
              this.$emit('success')
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    },

  }
</script>
